package com.cafebabe.mongo.service;

import com.alibaba.fastjson.JSONObject;

import com.cafebabe.bean.Result;
import com.cafebabe.mongo.dao.LogDao;
import com.cafebabe.mongo.dao.MongoDao;
import com.cafebabe.mongo.entity.Log;
import com.cafebabe.mongo.service.impl.MongoServiceImpl;
import com.cafebabe.util.IpUtil;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * Created by xForMe on 2015/7/22.
 */
@Service
public class LogService extends MongoServiceImpl<Log,String> implements MongoService<Log,String> {

    @Resource
    private LogDao logDao;

    @Override
    public MongoDao<Log, String> getMongoBaseDao() {
        return logDao;
    }



    /**
     * 存储log日志
     * @param actionName
     * @param methodName
     * @param request
     * @return
     */
    public Result saveLog(String actionName, String methodName, HttpServletRequest request) {
        Result result = new Result(200,"保存成功");
        String ip = IpUtil.getIpAddr(request);
        Map map = request.getParameterMap();
        Log log = new Log();
        log.setCreateDate(new Date());
        log.setActionName(actionName);
        log.setMethodName(methodName);
        log.setRequestURI(request.getRequestURI());
        log.setRequestParam(JSONObject.toJSONString(map));
        log.setRequestIp(ip);
        log.setHost(request.getHeader("Host"));
        log.setUserAgent(request.getHeader("User-Agent"));
        logDao.save(log);
        return result;

    }
}
